<?php

namespace Crawler;

use Zend\Dom\Query;

class Filter {

    public function __construct(Client $client, Query $cssQuery) {
        $this->client = $client;
        $this->cssQuery = $cssQuery;
    }

    /**
     * Lọc links từ chuyên mục
     * $url : địa chỉ chuyên mục
     * $template : thẻ html bao chứa links
     * $spec : ký tự đặc biệt trong links
     */
    public function getLinkTemp($url, $template, $spec) {
        return $this->client->request(self::GET_LINK_TEMP, array(
            'url' => urlencode($url),
            'prtt' => $template,
            'spec' => $spec,
        ));
    }

    /**
     * PHPCrawler
     */
    public function getLink($url, $prtt, $spec) {
        return $this->client->request(self::GET_LINK, array(
                    'url' => $url,
                    'prtt' => $prtt,
                    'spec' => $spec,
        ));
    }

    public function getGCContentNeedTemp($html, $template) {
        return $this->client->request(self::GET_GC_CONTENT_NEED_TEMP, array(
                    'html' => $html,
                    'template' => $template,
                        ), 'post');
    }

    /**
     * PHPCrawler
     */
    public function getGCContentNeed($html, $prtt) {
        return $this->client->request(self::GET_GC_CONTENT_NEED, array(
                    'html' => $html,
                    'prtt' => $prtt,
        ));
    }

    public function downloadUrl($url = '') {
        $html = $this->client->request($url, array(
            'url' => urlencode($url)
        ));
        return $html;
    }

    public function cssQuery($html = '', $selector = '') {
        $this->cssQuery->setDocumentHtml($html);
        $result = $this->cssQuery->execute($selector);
        return $result;
    }

    public function getSubContent($html, $element = '') {
        if (!$html)
            die(__FUNCTION__ . ' phát sinh lỗi do $html không hợp lệ');
        $part = explode('$', $element);

        if (count($part) !== 2 || !$part[0] || !$part[1])
            die('Chuỗi xác định không hợp lệ');

        $m = strpos($html, $part[0]);
        $n = strpos($html, $part[1], $m);

        if (!$m || !$n)
            die('Nội dung tìm kiếm không được tìm thấy');

        $start = strlen($part[0]) + $m;
        $length = $n - $start;


        $filter = new \stdClass;
        $filter->prtt = htmlentities(addslashes($element));
        $filter->cnt = strip_tags(substr($html, $start, $length));
        return json_encode($filter);
    }

    protected $cssQuery;
    protected $client;

    const GET_LINK_TEMP = 'http://tinhoc.net.vn:8875/submitGLTemp.php';
    const GET_GC_CONTENT_NEED_TEMP = 'http://27.118.18.200:8876/getGCContentNeedTemp';
    const GET_GC_CONTENT_NEED = 'http://27.118.18.200:8876/getGCContentNeed';
    const GET_LINK = 'http://27.118.18.200:8876/getLink';
    const DOWNLOAD_URL = 'http://27.118.18.200:8876/downloadUrl';

}
